/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package io.cordova.hellocordova;

import android.os.Bundle;
import org.apache.cordova.*;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.apache.cordova.*;
import cn.domob.android.ads.*;
import cn.domob.android.ads.AdManager.ErrorCode;

import com.umeng.analytics.MobclickAgent;

public class HelloCordova extends CordovaActivity
{
        public static final String PUBLISHER_ID     = "56OJwHaIuN8DAz4fkQ";
        public static final String InterstitialPPID = "16TLuWhoAp2gYNUIIjh9tFyk";

        public static HelloCordova content;
        public static  InterstitialAd mInterstitialAd;

        public static void sendMessage(int what, Object object) 
        {
                Message msg     = new Message();
                msg.what        = what;
                msg.obj         = object;
                handle.sendMessage(msg);  
        }

        private static Handler handle = new Handler() 
        {
                public void handleMessage(Message msg)
                {
                        if(msg.what == 0)
                        {
                                mInterstitialAd.showInterstitialAd(content);
                        }
                }
        };
  
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                // Set by <content src="index.html" /> in config.xml
                setIntegerProperty("splashscreen", R.drawable.screen); 
                loadUrl(launchUrl,5000);

                content = this;
                mInterstitialAd = new InterstitialAd(this, HelloCordova.PUBLISHER_ID, HelloCordova.InterstitialPPID);
                mInterstitialAd.loadInterstitialAd();
      
                new Thread( new Runnable() {
                        @Override  
                        public void run() 
                        {
                                try{
                                        Thread.sleep(10000);
                                        if (mInterstitialAd.isInterstitialAdReady()){
                                                sendMessage(0, null);
                                                Log.i("DomobSDKDemo", "====is Ok");
                                        }else {
                                                Log.i("DomobSDKDemo", "====Interstitial Ad is not ready");
                                        }
                                }catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }  
                }).start();

                // umeng
                MobclickAgent.setDebugMode(false);
                MobclickAgent.openActivityDurationTrack(false);
                MobclickAgent.updateOnlineConfig(this);
        }
    
        @Override
        public void onResume()
        {
                super.onResume();
                MobclickAgent.onResume(this);
        }

        @Override
        public void onPause() 
        {
                super.onPause();
                MobclickAgent.onPause(this);
        }
}
